<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------


use Swoole\WebSocket\Server;
use think\facade\Cache;

class WebsocketTest {

    public $server;

    public $redis;

    public $key;


    public function __construct() {

        $this->key = '122345446';

        if(empty($this->redis)){

            $this->redis = new Redis();

            $this->redis ->connect('127.0.0.1',6379);
        }


        $this->server = new Swoole\WebSocket\Server("0.0.0.0", 9501);

        $this->server->set(array(

            'reactor_num'   => 2, //reactor thread num

            'worker_num'    => 4,    //worker process num

            'backlog'       => 128,   //listen backlog

            'max_request'   => 50,

            'dispatch_mode' => 1,

//            'daemonize' => 1

        ));


        $this->server->on('open', function (swoole_websocket_server $server, $request) {

            //加入房间域
            $this->redis->hset($this->key,$request->fd,$request->fd);
            //加入组集合
            $this->redis->sadd('group', $this->key);

        });





        $this->server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
           // echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";

            $group = $this->redis->sMembers('group');

            foreach ($group as $v) {

                $fangjian = $this->redis->hGetAll($v);

                foreach ($fangjian as $k => $vv) {

                    $server->push($vv, count($fangjian).$frame->fd);

                }

            }


        });




        $this->server->on('close', function ($server, $fd) {

            $this->redis->hdel($this->key, 2);

            //echo "client {$fd} closed\n";
            //退出并删除多余的分组fd
            $group = $this->redis->sMembers('group');

            foreach ($group as $v) {

                $fangjian = $this->redis->hgetall($v);

                foreach ($fangjian as $k => $vv) {

//                    if ($fd == $vv) {

                        $this->redis->hdel($v, $vv);
//                    }
                }
            }

        });




//
        $this->server->on('request', function ($request, $response) {
            // 接收http请求从get获取message参数的值，给用户推送
            // $this->server->connections 遍历所有websocket连接用户的fd，给所有用户推送
            foreach ($this->server->connections as $fd) {
                // 需要先判断是否是正确的websocket连接，否则有可能会push失败
                if ($this->server->isEstablished($fd)) {
                    $this->server->push($fd, '11');
                }
            }
        });

        $this->server->start();
    }





}


new WebsocketTest();